<?php
//常数文件
include_once('define.php');

/*
 * @灯商云端
 * @功能说明：框架层多应用函数库
 * @更新说明：暂无更新
 * @文件名 function.php
 * @编码 UTF-8
 * @创建时间 2015-6-2 下午4:41:42
 * @创建人 XING
 */
/*
 * 存储用户信息
 *
 */
if(!function_exists('runCacheUser')){
    function runCacheUser($cust_id,$equipment) {
        if($cust_id) {
            $info = M("CustEntity")->where(array("cust_id"=>$cust_id))->find();
			$info['equipment']=$equipment ? :'android';//设备信息 android; apple;
			S('cust_msg_'.$cust_id,$info,8640000);
            return true;
        }
    }
}
/*
 * 时间戳格式化输出
 * @date: 2015-3-21上午10:55:01
 * @editor: XING
 */
if(!function_exists('format_date')){
	function format_date($time,$param='Y-m-d'){
		return $time ? date($param,$time) : '-';
	}
}

/*
 * JSON串统一返回出口
 * @date: 2015-3-21下午4:48:56
 * @editor: XING
 */
if(!function_exists('jsonRespons')){
	function jsonRespons($rcode,$msg){
		if(!$msg){
			echo json_encode($rcode);exit;
		}
		echo json_encode(array(
			'msg' => $msg,
			'code' => $rcode	
		));exit;
	}
}




/*
 * Sql查询Query缓存
 * @date: 2015-8-10 下午12:55:23
 * @editor: XING
 */
if(!function_exists('CQuery')){
	function CQuery($sql,$table=array()){
		$keyCache = md5(trim($sql));
		$checkReback = \Think\Model::selfRefreshs($table, $keyCache);
		if(S($keyCache) && $checkReback && C('open_cache')){
			return S($keyCache);
		}else{
			$dataBacked = (new \Think\Model())->query($sql);
			if($table){
				S(md5($sql),$dataBacked,null,$table);
				return $dataBacked;
			}else{
				return $dataBacked;
			}
		}
	}
}

/*
 * SOCK协议获取采集
 * @date: 2015-3-27上午9:36:36
 * @editor: XING
 */
if(!function_exists('get_page_content')){
	function get_page_content($url){
		$url = @eregi_replace('^http://', '', $url);
		$temp = explode('/', $url);
		$host = array_shift($temp);
		$path = '/'.implode('/', $temp);
		$temp = explode(':', $host);
		$host = $temp[0];
		$port = isset($temp[1]) ? $temp[1] : 80;
		$fp = @fsockopen($host, $port, $errno, $errstr, 30);
		if ($fp){
			@fputs($fp, "GET $path HTTP/1.1\r\nHost: $host\r\nAccept: */*\r\nReferer:$url\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\nConnection: Close\r\n\r\n");
		}
		$Content = '';
		while ($str = @fread($fp, 4096)){
			$Content .= $str;
		}
		@fclose($fp);
		//重定向
		if(preg_match("/^HTTP\/\d.\d 301 Moved Permanently/is",$Content)){
			if(preg_match("/Location:(.*?)\r\n/is",$Content,$murl)){
				return get_page_content($murl[1]);
			}
		}
		//读取内容
		if(preg_match("/^HTTP\/\d.\d 200 OK/is",$Content)){
			preg_match("/Content-Type:(.*?)\r\n/is",$Content,$murl);
			$contentType=trim($murl[1]);
			$Content=explode("\r\n\r\n",$Content,2);
			$Content=$Content[1];
		}
		
		return $Content;
	}
}
/*
 * 翻页功能
 * @date: 2015-3-28下午1:44:31
 * @editor: XING
 */
if(!function_exists('setPage')){
	function setPage($num, $perpage, $curpage, $mpurl)
	{
		$page = 10;
		$mpurl .= strpos ( $mpurl, '?' ) !== false ? '&' : '?';
		I('get.res') ? $mpurl .= 'res='.I('get.res').'&' : '';
		$realpages = 1;
		if ($num > $perpage) {
	
			$offset = 2;
			$realpages = @ceil ( $num / $perpage );
			$pages = $realpages;
			if ($page > $pages) {
				$from = 1;
				$to = $pages;
			} else {
				$from = $curpage - $offset;
				$to = $from + $page - 1;
				if ($from < 1) {
					$to = $curpage + 1 - $from;
					$from = 1;
					if ($to - $from < $page) {
						$to = $page;
					}
				} elseif ($to > $pages) {
					$from = $pages - $page + 1;
					$to = $pages;
				}
			}
//			$multipage = '<div class="pagination alternate"><ul class="pagination">';
//			$url = $mpurl.'page=';
//			$multipage .= '<li '.($curpage == 1 ? 'class="disabled"' : '').'><a href="'.($curpage == 1 ? 'javascript:void(0);' : $url.($curpage - 1)).'">上一页</a></li>';
//			for($i = $from; $i <= $to; $i ++) {
//				$multipage .= '<li '.($i == $curpage ? 'class="active"' : '').'><a href="'.$url.$i.'">'.$i.'</a></li>';
//			}
//			$multipage .= '<li '.($curpage == $pages ? 'class="disabled"' : '').'><a href="'.($curpage == $pages ? 'javascript:void(0)' : $url.($curpage + 1)).'">下一页</a></li>';
//			$multipage .= '</ul></div>';

            $multipage = '<div class="define-paging clearfix">';
            $multipage .= '<div class="pull-left">共'.$realpages.'页</div>';
            $url = $mpurl.'page=';
            $multipage .= '<div class="pull-left" id="the_first_page"><a href="'.($curpage == 1 ? 'javascript:void(0);' : $url).'">首页</a></div>';
			$multipage .= '<div class="pull-left" '.($curpage == 1 ? 'class="disabled"' : '').'><a href="'.($curpage == 1 ? 'javascript:void(0);' : $url.($curpage - 1)).'">上一页</a></div>';
			for($i = $from; $i <= $to; $i ++) {
				$multipage .= '<div class="pull-left" '.($i == $curpage ? 'class="define-active"' : '').'><a href="'.$url.$i.'">'.$i.'</a></div>';
			}
			$multipage .= '<div class="pull-left" '.($curpage == $pages ? 'class="disabled"' : '').'><a href="'.($curpage == $pages ? 'javascript:void(0)' : $url.($curpage + 1)).'">下一页</a></div>';
			$multipage .= '<div class="pull-left" id="the_last_page"><a href="'.$url.$realpages.'">尾页</a></div>';
			$multipage .= '</div>';
		}
	
		return $multipage;
	}
}
/*
 * 短信发送模块
 * @date: 2015-4-8下午1:58:27
 * @editor: XING
 * 返回值 成功(true),失败(false)
 */
if(!function_exists('sendsms')){
	function sendsms($phones,$content){
		//发送短信接口
		$statusCode = (new \service\message\Message())->sendMessage($phones,$content);
		
		//@todo 记录短信日志
		if($statusCode!=null && $statusCode==0){
			return array(
				'error' => '0',
				'msg' => '短信发送成功'
			);
		}else{
			return array(
					'error' => $statusCode,
					'msg' => '发送失败'
				);
		}
	}
}
/*
 * 短信查询余额接口
 * @date: 2015-4-8下午3:25:59
 * @editor: XING
 */
if(!function_exists('getBlance')){
	function getBlance(){
		return (new \service\yimei\YiMei())->getBlance();
	}
}
/*
 * 图片上传封装函数
 * @date: 2015-4-9上午11:17:38
 * @editor: XING
 */
if(!function_exists('upimgs')){
	function upimgs($files,$filename='imgFile',$dir='',$ext=''){
		return (new \service\upload\UpLoad())->upimgs($files,$filename,$dir,$ext);
	}
}
/*
 * 图片URL统一转化
 * @date: 2015-4-10下午4:34:24
 * @editor: XING
 */
if(!function_exists('transUrl')){
	function transUrl($url){
		return (strstr($url,'http:') || strstr($url,'https:') || strstr($url,'.com')) ? $url : ($url ? IMGHOST.$url : '');
	}
}
/*
 * 获取系统配置参数
 * @date: 2015-4-23上午11:29:01
 * @editor: XING
 */
if(!function_exists('getSysConfig')){
	function getSysConfig($param){
		return M('ConfigSet')->getFieldByConfigColumn($param,'config_val');
	}
}
/*
 * CURL操作
 * @date: 2015-6-24 上午10:56:18
 * @editor: XING
 */
if(!function_exists('curl_get')){
	function curl_get($url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$dom = curl_exec($ch);
		curl_close($ch);
		return $dom;
	}
}
/*
 * CURL异步请求
 * @date: 2015-7-6 上午11:24:29
 * @editor: XING
 */
if(!function_exists('curl')){
	function curl($action,$params=array(),$httpHeader=array(),$format='') {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $action);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); //处理http证书问题
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$ret = curl_exec($ch);
		if (false === $ret) {
			$ret =  curl_errno($ch);
		}
		curl_close($ch);
		if ($format=='json') {
			$ret = json_decode($ret);
		}
		return $ret;
	}
}



/*
 * 中文首字母
 * @date: 2015-7-28 上午10:50:06
 * @editor: XING
 */
if(!function_exists('pykey')){
	function pykey( $str)
	{
		if(empty($str)){return '';}
		
		$fchar=ord($str{0});
		
		if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str{0});
		
		$s1=iconv('UTF-8','gb2312',$str);
		
		$s2=iconv('gb2312','UTF-8',$s1);
		
		$s=$s2==$str?$s1:$str;
		
		$asc=ord($s{0})*256+ord($s{1})-65536;
		
		if($asc>=-20319&&$asc<=-20284) return 'A';
		
		if($asc>=-20283&&$asc<=-19776) return 'B';
		
		if($asc>=-19775&&$asc<=-19219) return 'C';
		
		if($asc>=-19218&&$asc<=-18711) return 'D';
		
		if($asc>=-18710&&$asc<=-18527) return 'E';
		
		if($asc>=-18526&&$asc<=-18240) return 'F';
		
		if($asc>=-18239&&$asc<=-17923) return 'G';
		
		if($asc>=-17922&&$asc<=-17418) return 'H';
		
		if($asc>=-17417&&$asc<=-16475) return 'J';
		
		if($asc>=-16474&&$asc<=-16213) return 'K';
		
		if($asc>=-16212&&$asc<=-15641) return 'L';
		
		if($asc>=-15640&&$asc<=-15166) return 'M';
		
		if($asc>=-15165&&$asc<=-14923) return 'N';
		
		if($asc>=-14922&&$asc<=-14915) return 'O';
		
		if($asc>=-14914&&$asc<=-14631) return 'P';
		
		if($asc>=-14630&&$asc<=-14150) return 'Q';
		
		if($asc>=-14149&&$asc<=-14091) return 'R';
		
		if($asc>=-14090&&$asc<=-13319) return 'S';
		
		if($asc>=-13318&&$asc<=-12839) return 'T';
		
		if($asc>=-12838&&$asc<=-12557) return 'W';
		
		if($asc>=-12556&&$asc<=-11848) return 'X';
		
		if($asc>=-11847&&$asc<=-11056) return 'Y';
		
		if($asc>=-11055&&$asc<=-10247) return 'Z';
		
		return null;
	}
}
	
/*
 * Xml转Array
 * @date: 2015-8-15 下午12:59:33
 * @editor: XING
 */
if(!function_exists('xmlToArray')){
	function xmlToArray($str){
		//$str='<xml><node>1111</node><aa>78</aa></xml>';
		$res = @simplexml_load_string($str,NULL,LIBXML_NOCDATA);
		$res = json_decode(json_encode($res),true);
		
		return $res;
	}
}
	
/*
 * Array转Xml
 * @date: 2015-8-15 下午1:01:07
 * @editor: XING
 */
if(!function_exists('arrayToXml')){
	function arrayToXml($ar){
		$doc = new \DOMDocument('1.0','UTF-8');
		$doc->formatOutput = true;
		$root = $doc->createElement('request');
		$root = $doc->appendChild($root);
		foreach($ar as $title=>$title_v){
			$title = $doc->createElement($title);
			$title = $root->appendChild($title);
			if(is_array($title_v)){
				foreach($title_v as $k=>$v){
					$k = $doc->createElement($k);
					$k = $title->appendChild($k);
					$text = $doc->createTextNode($v);
					$text = $k->appendChild($text);
				}
			}else{
				$text = $doc->createTextNode($title_v);
				$text = $title->appendChild($text);
			}
		}
		
		echo $doc->saveXML();exit;
	}
}

/*
 * Excel导出功能
 * @date: 2015-12-18 下午12:36:51
 * @editor: XING
 */
if(!function_exists('loadExcel')){
	function loadExcel($fileName = '导出EXCEL',$data = array()){
		if(empty($data))
			exit('导出文件参数有误');
		$dataHeader = $data[0];unset($data[0]);
		(new \service\excel\PhpExcel())->loadExcel($fileName,$dataHeader,$data);
	}
}


/*
 * Excel导出功能
 * @date: 2016-04-28 12:13:00
 * @editor: WENCUN
 * */
if(!function_exists('exportExcel')){
	function exportExcel($tableHeader,$data=array()){
		return (new \service\excel\PhpExcel())->exportExcel($tableHeader,$data);
	}
}


/*
 * Excel导入功能
 * @date: 2016-04-19 18:32:00
 * @editor: WENCUN
 * */
if(!function_exists('importExcel')){
	function importExcel($url){
		if(!file_exists($url)){
			exit('文件目录不存在');
		}
		return (new \service\excel\PhpExcel())->importExcel($url);
	}
}

/*
 * 发送短信功能
 * @date: 2016-12-08 13:17:00
 * @editor: liuqi
 * */
if(!function_exists('sendMsg')){
	function sendMsg($mobile,$code){
		$url = 'http://sms.253.com/msg/send?un=N7960671&pw=M58kjqV34Jee80&phone='.$mobile.'&msg=【虞美人】本次验证码为'.$code.'，10分钟内有效&rd=1';
		return curl_get($url);
	}
}

/*
 * 友盟推送功能
 * @date: 2016-12-08 18:32:00
 * @editor: yanyan
 * */
if(!function_exists('umeng_push')){
    function umeng_push($appkey,$secret,$type,$uid,$title,$text){
        $umeng = new \Org\Util\Umeng($appkey, $secret);
        if ($uid){
            $type == 'android'?$umeng->sendAndroidCustomizedcast($uid,$title,$text):$umeng->sendIOSCustomizedcast($uid,$text);
        }else{
            $type == 'android'?$umeng->sendAndroidBroadcast($title,$text):$umeng->sendIOSBroadcast($text);
        }
    }
}

?>